﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Generator.AddImportCodeGenerator
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using Microsoft.Internal.Web.Utils;
using System.CodeDom;
using System.Collections;
using System.Linq;
using System.Web.Razor.Parser.SyntaxTree;

namespace System.Web.Razor.Generator
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Represents the added import code generator for the razor.</summary>
  public class AddImportCodeGenerator : SpanCodeGenerator
  {
    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Generator.AddImportCodeGenerator" /> class.</summary>
    /// <param name="ns">The string namespace.</param>
    /// <param name="namespaceKeywordLength">The length of the keyword namespace.</param>
    public AddImportCodeGenerator(string ns, int namespaceKeywordLength)
    {
      this.Namespace = ns;
      this.NamespaceKeywordLength = namespaceKeywordLength;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the string namespace of the generator to add import code generator.</summary>
    /// <returns>The string namespace of the generator to add import code generator.</returns>
    public string Namespace { get; private set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the length of keyword namespace for the code generator.</summary>
    /// <returns>The length of keyword namespace for the code generator.</returns>
    public int NamespaceKeywordLength { get; set; }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Generates code with the specified parameters using the added import code generator.</summary>
    /// <param name="target">The target span.</param>
    /// <param name="context">The code generator context.</param>
    public override void GenerateCode(Span target, CodeGeneratorContext context)
    {
      string ns = this.Namespace;
      if (!string.IsNullOrEmpty(ns) && char.IsWhiteSpace(ns[0]))
        ns = ns.Substring(1);
      CodeNamespaceImport codeNamespaceImport = context.Namespace.Imports.OfType<CodeNamespaceImport>().Where<CodeNamespaceImport>((Func<CodeNamespaceImport, bool>) (i => string.Equals(i.Namespace, ns.Trim(), StringComparison.Ordinal))).FirstOrDefault<CodeNamespaceImport>();
      if (codeNamespaceImport == null)
      {
        codeNamespaceImport = new CodeNamespaceImport(ns);
        context.Namespace.Imports.Add(codeNamespaceImport);
      }
      codeNamespaceImport.LinePragma = context.GenerateLinePragma(target);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Returns a string that represents the current object.</summary>
    /// <returns>A string that represents the current object.</returns>
    public override string ToString() => "Import:" + this.Namespace + ";KwdLen:" + (object) this.NamespaceKeywordLength;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Determines whether two object instances are equal.</summary>
    /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
    /// <param name="obj">The object to compare with the current object.</param>
    public override bool Equals(object obj) => obj is AddImportCodeGenerator importCodeGenerator && string.Equals(this.Namespace, importCodeGenerator.Namespace, StringComparison.Ordinal) && this.NamespaceKeywordLength == importCodeGenerator.NamespaceKeywordLength;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Returns the hash code for this instance.</summary>
    /// <returns>The hash code for this instance.</returns>
    public override int GetHashCode() => HashCodeCombiner.Start().Add((IEnumerable) this.Namespace).Add(this.NamespaceKeywordLength).CombinedHash;
  }
}
